<template>
  <div class="login">
    <div style="background: #2c3e50; width: 700px; margin: 60px auto; padding: 20px 50px">
      <h1 style="margin: 30px auto;text-align: center;color: white">用户登录</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" show-password>
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>


        <div style="margin-left: 160px ">
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </div>


        <div style="margin-left: 400px ">
          <el-form-item>
            <el-button @click="registerForm()" size="mini">注册</el-button>
            <el-button @click="findForm()" size="mini">忘记密码</el-button>
          </el-form-item>
        </div>


      </el-form>


    </div>
  </div>
</template>

<style>
body {

}
</style>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名称', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 18, message: '长度在 3 到 18 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    registerForm() {
      this.$router.replace('/sys-admin/user/register');
    },
    findForm() {
      this.$router.replace('/sys-admin/user/find');
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {

          let url = 'http://localhost:9080/user/login';
          console.log('url = ' + url);


          let formData = this.qs.stringify(this.ruleForm);

          console.log(formData);

          this.axios.post(url, formData).then((response) => {
            let jsonResult = response.data;
            console.log(jsonResult);
            if (jsonResult.state == 20000) {
              this.$message({
                showClose: true,
                message: '登录成功！',
                type: 'success'
              });

              if (this.ruleForm.username === "管理员") {

                this.$router.push('/admin');

              } else {

                this.$router.push('/about');

              }

            } else {
              this.$alert(jsonResult.message, '操作失败', {
                confirmButtonText: '确定',
                callback: action => {
                  this.resetForm('ruleForm');
                }
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

